﻿using AutoMapper;
using MediatR;
using SheepStorage.API.Application.Command.SheepPurchaseCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;

namespace SheepStorage.API.Application.CommandHandlers.SheepPurchaseHandler
{
    public class SheepPurchaseUpdateHandler:IRequestHandler<SheepPurchaseUpdateCommand,Results<int>>
    {
        private readonly ISheepPurchaseService _purchaseService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchaseService"></param>
        /// <param name="mapper"></param>
        public SheepPurchaseUpdateHandler(ISheepPurchaseService purchaseService,IMapper mapper)
        {
            _purchaseService = purchaseService;
            _mapper = mapper;
        }
        /// <summary>
        /// 羊只采购修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Results<int>> Handle(SheepPurchaseUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = _mapper.Map<SheepPurchase>(request);

            return Results<int>.DataResult(await _purchaseService.UpdateSheepPurchase(result));
        }
    }
}
